﻿using System;
using System.Windows.Forms;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.Manager;


namespace SdmxMl.UC
{
    public partial class UcCode : UcBase
    {
        private CodeType c;
        public UcCode()
        {
            InitializeComponent();
            comboBoxTitle.DropDownControl = ucMlText;
        }

        public override bool BindFromNode(TreeNode node, bool isFinal)
        {
            c = null;
            base.BindFromNode(node, isFinal);
            if (node != null)
                c = node.Tag as CodeType;
            BindToControls();
            IsFinalEditControls(isFinal);

            return Visible;
        }

        private void BindToControls()
        {
            if (c != null)
            {
                Visible = true;

                textBoxValue.Text = c.Id;
                textBoxUrn.Text = c.Urn == null ? string.Empty : c.Urn.ToString();
                textBoxUri.Text = c.Uri;
                comboBoxTitle.Text = c.Name.ToString();
                ucMlText.ML = c.Name;

                comboMultiLinesDescr.Text = c.Description.ToString();
                comboMultiLinesDescr.ML = c.Description;

                FillParentInfo(textBoxParent, textBoxParentCodeDescr);

                // External Navigation URI Command
                buttonNavigateUri.Visible = string.IsNullOrEmpty(c.Uri) == false;
            }
            else
                Visible = false;
        }

        protected override bool BindFromControls()
        {
            bool err = false;
            if (c != null)
            {
                err = CheckUniqueId(textBoxValue);
                if (UrlValidator.IsValidUrl(textBoxUrn.Text))
                    c.Urn = new Uri(textBoxUrn.Text);

                c.Name.UpdateForCurrentLanguage(comboBoxTitle.Text);
                c.Description.UpdateForCurrentLanguage(comboMultiLinesDescr.Text);
            }

            return err;
        }

        // No edition if Parent Artefact IsFinal
        private void IsFinalEditControls(bool isFinal)
        {
            bool enabled = isFinal == false;

            textBoxValue.ReadOnly = isFinal;
            comboBoxTitle.IsFinal = isFinal;
            comboMultiLinesDescr.IsFinal = isFinal;
            textBoxUrn.ReadOnly = isFinal;
            textBoxUri.ReadOnly = isFinal;
        }

        private void textBoxValue_Leave(object sender, EventArgs e)
        {
            CheckUniqueId(textBoxValue);
        }

        private void buttonNavigateUri_Click(object sender, EventArgs e)
        {
            try
            {
                using (new SdmxMl.Helper.CWaitCursor())
                {
                    // If URN try to get it if not already in memory
                    ArtefactRefTyped art = UrnManager.UrnToArtefactRefTyped(c.Uri);
                    if (art != null)
                    {
                        UrnParser p = new UrnParser(c.Uri);

                        // If already in memory, navigate otherwhile get from regidtry
                        if (NavigationManager.LocatenavigateProject(art.Type, art.FullIdent, p) == null)
                            NavigationManager.NavigateWithRegistry(art.Type, art.FullIdent, true, p);
                    }
                    else
                    {
                        // Try to process document
                        System.Diagnostics.Process myProc;
                        myProc = System.Diagnostics.Process.Start(c.Uri);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void textBoxUri_TextChanged(object sender, EventArgs e)
        {
            // External Navigation Command
            c.Uri = textBoxUri.Text;
            buttonNavigateUri.Visible = textBoxUri.Text.Length > 0;

        }

    }
}
